import bb.cascades 1.2
import "../lib"
import bb.WebImageView 1.4

Container {
    id: focusRoot
    property variant focusItems

    onFocusItemsChanged: {
        var items = [];
        for(var i=focusItems.length-1;i>=0;i--){
            var item = focusItems[i];
            item["itemIndex"] = (i+1) + "/"+focusItems.length;
            console.log(JSON.stringify(item))
            
            items.push(item)
        }
        theDataModel.insertList(items)
    }
    
    ListView {
        id: view

        dataModel: GroupDataModel {
            id: theDataModel
            grouping: ItemGrouping.None
        }

        layout: StackListLayout {
            orientation: LayoutOrientation.LeftToRight
            headerMode: ListHeaderMode.None
        }

        snapMode: SnapMode.LeadingEdge
        flickMode: FlickMode.SingleItem
        scrollIndicatorMode: ScrollIndicatorMode.ProportionalBar
        stickToEdgePolicy: ListViewStickToEdgePolicy.BeginningThenEnd
        layoutProperties: StackLayoutProperties {
        }

        onTriggered: {
            var currentItem = dataModel.data(indexPath);
            var nav = currentItem.navRoot;
            
            if(currentItem.type == "doc"){
                var docCom = Qt.createComponent("asset:///ViewDoc.qml")
                var docView = docCom.createObject(nav);
                docView.item = currentItem
                nav.push(docView);
            }else if(currentItem.type == "slide"){
                var docCom = Qt.createComponent("asset:///ViewSlider.qml")
                var docView = docCom.createObject(nav);
                docView.url = currentItem.id;
                docView.open();
            }
        }

        scrollRole: ScrollRole.None

        listItemComponents: [
            ListItemComponent {
                type: "item"
                FocusSubItem {
                    width: ListItemData.screenWidth
                    height: 360 * (width / 640)
                    title: ListItemData.title
                    itemIndex: ListItemData.itemIndex
                    thumbnail: ListItemData.thumbnail
                }
            }
        ]
    }
}
